package com.student.teacher.controller;

import com.student.common.entity.Result;
import com.student.common.entity.Teacher;
import com.student.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 教师控制器
 * 
 * @author student
 * @date 2025
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    
    @Autowired
    private TeacherService teacherService;
    
    /**
     * 教师列表查询
     */
    @GetMapping("/list")
    public Result<Page<Teacher>> getTeacherList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String employeeId,
            @RequestParam(required = false) String status) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<Teacher> teachers = teacherService.getTeacherList(pageable, name, title, department, employeeId, status);
            return Result.success(teachers);
        } catch (Exception e) {
            return Result.error("查询教师列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询教师
     */
    @GetMapping("/{id}")
    public Result<Teacher> getTeacherById(@PathVariable String id) {
        try {
            Teacher teacher = teacherService.getTeacherById(id);
            if (teacher != null) {
                return Result.success(teacher);
            } else {
                return Result.error("教师不存在");
            }
        } catch (Exception e) {
            return Result.error("查询教师失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增教师
     */
    @PostMapping
    public Result<String> addTeacher(@RequestBody Teacher teacher) {
        try {
            boolean success = teacherService.addTeacher(teacher);
            if (success) {
                return Result.success("新增教师成功");
            } else {
                return Result.error("新增教师失败，工号已存在");
            }
        } catch (Exception e) {
            return Result.error("新增教师失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新教师信息
     */
    @PutMapping("/{id}")
    public Result<String> updateTeacher(@PathVariable String id, @RequestBody Teacher teacher) {
        try {
            teacher.setTeacherId(id);
            boolean success = teacherService.updateTeacher(teacher);
            if (success) {
                return Result.success("更新教师信息成功");
            } else {
                return Result.error("更新教师信息失败，教师不存在");
            }
        } catch (Exception e) {
            return Result.error("更新教师信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除教师
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteTeacher(@PathVariable String id) {
        try {
            boolean success = teacherService.deleteTeacher(id);
            if (success) {
                return Result.success("删除教师成功");
            } else {
                return Result.error("删除教师失败，教师不存在或有授课记录");
            }
        } catch (Exception e) {
            return Result.error("删除教师失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取教师总数
     */
    @GetMapping("/count")
    public Result<Long> getTeacherCount() {
        try {
            long count = teacherService.getTeacherCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取教师总数失败：" + e.getMessage());
        }
    }
}
